package com.yx.yinshanjinniu.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.adapters.SignAdapter;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.bean.SignBean;
import com.yx.yinshanjinniu.bean.UserInfoBean;
import com.yx.yinshanjinniu.eventbus.EventCenter;
import com.yx.yinshanjinniu.eventbus.EventConstants;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.ToastUtil;
import com.yx.yinshanjinniu.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

public class SignActivity extends BaseActivity {
    private SignAdapter mSignAdapter;
    private List<SignBean> mSignList = new ArrayList<>();
    private MyGridView gv_sign;
    private ImageView iv_back;
    private TextView tv_integral, tv_sign;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData() {
        UserInfoBean bean = DataUtils.getUserInfo(mContext);
        tv_integral.setText(bean.integral + "");
        mAppAction.getSignDataList(new ActionCallbackListener<List<SignBean>>() {
            @Override
            public void onSuccess(List<SignBean> data) {
                mSignList.clear();
                mSignList.addAll(data);
                mSignAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_sign;
    }

    @Override
    protected void findViews() {
        tv_integral = $(R.id.tv_integral);
        tv_sign = $(R.id.tv_sign);
        gv_sign = $(R.id.gv_sign);
        iv_back = $(R.id.iv_back);

        mSignAdapter = new SignAdapter(mContext, mSignList);
        gv_sign.setAdapter(mSignAdapter);
    }

    @Override
    protected void setListeners() {
        tv_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfoBean bean = DataUtils.getUserInfo(mContext);
                if (bean.isSign == 0) {//未签到
                    userSign();
                } else {//已签到
                    ToastUtil.showToast(mContext, "今日已签到");
                }
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void userSign() {
        mAppAction.signPost(new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {//更新用户数据
                ToastUtil.showToast(mContext, "签到成功");
                myApplication.updateUserDetail();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {
        super.onEventComming(eventCenter);
        switch (eventCenter.getEventCode()) {
            case EventConstants.EVENTCODE_UPDATE_USER://更新数据
                initData();
                break;
        }
    }
}